package com.carrental.api.common.exception;

import com.carrental.common.exception.OperationException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * 断言，取代繁琐重复的if判空，并抛出相关异常
 * @author LiangYi
 */
@Slf4j
public class Assert {

    /**
     * 断言对象不为空
     * obj 为空则抛异常
     */
    public static void notNull(Object obj, String message){
        if(obj == null){
            log.info("obj is null.....................");
            throw new OperationException(message);
        }
    }


    /**
     * 断言对象为空
     * 如果对象obj不为空，则抛出异常
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            log.info("obj is not null......");
            throw new OperationException(message);
        }
    }

    /**
     * 断言表达式为真
     * 如果不为真，则抛出异常
     *
     * @param expression 是否成功
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            log.info("fail...............");
            throw new OperationException(message);
        }
    }

    /**
     * 断言表达式为假
     * 如果不为假，则抛出异常
     *
     * @param expression 是否成功
     */
    public static void isFalse(boolean expression, String message) {
        log.info("status:"+expression);
        if (expression) {
            log.info("fail...............");
            throw new OperationException(message);
        }
    }

    /**
     * 断言两个对象不相等
     * 如果相等，则抛出异常
     */
    public static void notEquals(Object m1, Object m2,  String message) {
        if (m1.equals(m2)) {
            log.info("equals...............");
            throw new OperationException(message);
        }
    }

    /**
     * 断言两个对象相等
     * 如果不相等，则抛出异常
     */
    public static void equals(Object m1, Object m2,  String message) {
        if (!m1.equals(m2)) {
            log.info("not equals...............");
            throw new OperationException(message);
        }
    }

    /**
     * 断言参数不为空
     * 如果为空，则抛出异常
     */
    public static void notEmpty(String s, String message) {
        if (StringUtils.isEmpty(s)) {
            log.info("is empty...............");
            throw new OperationException(message);
        }
    }

}
